package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.dao.impl.NavImpl;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.INavService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class NavServiceImpl implements INavService {
    @Override
    public List<NavModel> getNavModelList() {
        Jedis jedis = new Jedis();//得到缓存的对象
        List<NavModel> list = new ArrayList<>();
        if (jedis.exists("navTitle")) {
            List<String> navTitle = jedis.lrange("navTitle", 0, -1);
            List<String> navHref = jedis.lrange("navHref", 0, -1);
            for (int i = 0; i < navHref.size(); i++) {
                NavModel model = new NavModel();
                model.setTitle(navTitle.get(i));
                model.setHref(navHref.get(i));
                list.add(model);
            }
        } else {
            NavDao navDao = new NavImpl();
            list = navDao.getNavModelList();
            for (NavModel model : list
            ) {
                jedis.rpush("navTitle", model.getTitle());
                jedis.rpush("navHref", model.getHref());
            }

        }
        return list;
    }

    public int update(NavModel navModel) {
        NavDao navDao = new NavImpl();
        int result = navDao.update(navModel);
        if (result == 1) {
            Jedis jedis = new Jedis();
            jedis.del("navTitle", "navHref");
        }
        return result;
    }
}
